﻿using System;
using System.Collections.Generic;
using System.Linq;
using CCWP7.Model;
using System.Diagnostics;
using Caliburn.Micro;

namespace CCWP7.CaliburnMicro
{
    public class ReadServer : IResult
    {
        readonly IServerDataManager _serverDataManager;

        public ReadServer (IServerDataManager serverDataManager)
        {
            _serverDataManager = serverDataManager;

            Server = new Server();
        }

        public void Execute(ActionExecutionContext context)
        {
            try
            {
                _serverDataManager.ReadAndUpdateServer(Server, s => Completed(this, new ResultCompletionEventArgs()));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());

                Completed(this, new ResultCompletionEventArgs() { Error = ex });
            }
        }

        public event EventHandler<ResultCompletionEventArgs> Completed = delegate
        {
        };

        public Server Server { get; private set; }
    }
}
